var $page = 0;

function isLoading( $name, $loading ){
	if( $loading )
		$( $name ).removeClass( 'hidden' );
	else
		$( $name ).addClass( 'hidden' );
}

function checkCommentsLeft(){
	$loaderButton = $( '#comments-button-loading' ); 
	$maxCount = parseInt( $loaderButton.attr( 'data-count_comments' ) );
	$currentCount = parseInt( $loaderButton.attr( 'data-current_count_comments' ) );
	$commsPerPage = $currentCount / $page;
	
	$currentCount += parseInt( $commsPerPage );
	
	if( $currentCount >= $maxCount ) $loaderButton.remove();
	else $loaderButton.attr( 'data-current_count_comments', $currentCount ); 
}

function getComments( $recipe_id ){
	$loadingName = '#comments-loading';
	isLoading( $loadingName, true );
	
	$.ajax( { 
        method		: 'POST',
        url       	: '/comment/get-comments', 
        data      	: { recipe_id : $recipe_id, page : $page + 1 },
        success   	: function( response ){
        	$page++;
        	$( '#comments-button-loading' ).before( response );
        	checkCommentsLeft();
        	isLoading( $loadingName, false );
        },
        error		: function( response ){
        	isLoading( $loadingName, false );
        }
    } );
}

function sendAnswer( element ){
	$( element ).parent().parent().submit();
}

function getAnswers( $recipe_id, $parent_comment ){
	$loadingName = '#answers-loading-' + $parent_comment;
	isLoading( $loadingName, true );
	$( '#answers-load-button-' + $parent_comment ).hide();
	
	$.ajax( { 
        method		: 'POST',
        url       	: '/comment/get-answers', 
        data      	: { recipe_id : $recipe_id, parent_comment : $parent_comment },
        success   	: function( response ){
        	$( '#comment-answers-container-' + $parent_comment ).append( response );
        	$( '#answers-load-button-' + $parent_comment ).remove();
        	isLoading( $loadingName, false );
        },
        error		: function( response ){
        	$( '#answers-load-button-' + $parent_comment ).show();
        	isLoading( $loadingName, false );
        }
    } );
}

function insertAnswer( form ){
	if( form.comment.value.length < 10 ){
		$( '#answer-length-' + form.parent_comment.value, form ).css( 'color', 'red' );
	} 
	
    $.ajax({
        url     : $(form).attr( 'action' ),
        type    : $(form).attr( 'method' ),
        data    : $(form).serialize(),
        success : function( response ) {
        	try{
        		JSON.parse( response );
        	}catch( err ){
        		$li = $( response );
        		$commentCount = $( '#comment-count-' +  form.parent_comment.value ); 
        		$li.hide();
        	    $( '.answer-text-' + form.parent_comment.value ).after( $li );
        	    $li.fadeIn(800);
        	    form.comment.value = '';
        	    $( '#answer-length-' + form.parent_comment.value, form ).html( 10 );
        	    $commentCount.text( parseInt( $commentCount.text().trim() ) + 1 );
        	}
        },
        error   : function( response ) {
                    alert('Error');     
        }
    } );
    return false;
}

function insertComment( form ){
	if( form.comment.value.length < 10 ){
		$( '#comment-length', form ).css( 'color', 'red' );
	} 
	
    $.ajax({
        url     : $(form).attr( 'action' ),
        type    : $(form).attr( 'method' ),
        data    : $(form).serialize(),
        success : function( response ) {
        	try{
        		JSON.parse( response );
        	}catch( err ){
        		$li = $( response );
        		$li.hide();
        	    $( '#new-comment-content' ).after( $li );
        	    $li.fadeIn(800);
        	    form.comment.value = '';
        	    
        		$( '#comment-length', form ).html( 10 );
        		$( '#comment-length', form ).css( 'color', '' );
        	}
        },
        error   : function( response ) {
                    alert('Error');     
        }
    } );
    return false;
}

function checkCommentLength( comment, counter ){
	if( comment.length < 10 ){
		$( counter ).css( 'color', '' );
		$( counter ).text( 10 - comment.length );
		$( counter ).html();
	}else if( comment.length > 245 ){
		$( counter ).css( 'color', 'red' );
		$( counter ).html( '<i class="fa fa-check"></i>' + ( 255 - comment.length ) );
	}else{
		$( counter ).css( 'color', 'green' );
		$( counter ).text( '' );
		$( counter ).html( '<i class="fa fa-check"></i>' );
	}
}

function likeDislike( recipe_id, comment_id, like ){
	$.ajax({
        url     : '/comment/like-dislike',
        type    : 'POST',
        data    : { recipe_id : recipe_id, comment_id : comment_id, like : like },
        success : function( response ) {
    		$like = $( '#like-' + comment_id );
    		$likeIcon = $( '#like-icon-' + comment_id );
    		$dislike = $( '#dislike-' + comment_id );
    		$dislikeIcon = $( '#dislike-icon-' + comment_id );
        	
        	if( response == 1 ){
        		if( like == 1 ){
        			$like.text( parseInt( $like.text().trim() ) + 1 );
        			$dislike.text( parseInt( $dislike.text().trim() ) - 1 );
        			$likeIcon.removeClass().addClass( 'fa fa-thumbs-up' );
        			$dislikeIcon.removeClass().addClass( 'fa fa-thumbs-o-down' );
        		}else{
        			$like.text( parseInt( $like.text().trim() ) - 1 );
        			$likeIcon.removeClass().addClass( 'fa fa-thumbs-o-up' );
        			$dislike.text( parseInt( $dislike.text().trim() ) + 1 );
        			$dislikeIcon.removeClass().addClass( 'fa fa-thumbs-down' );
        		}
        	}else if( response == 2 ){
        		if( like == 1 ){
        			$like.text( parseInt( $like.text().trim() ) + 1 );
        			$likeIcon.removeClass().addClass( 'fa fa-thumbs-up' );
        		}else{
        			$dislike.text( parseInt( $dislike.text().trim() ) + 1 );
        			$dislikeIcon.removeClass().addClass( 'fa fa-thumbs-down' );
        		}
        	}else{
        		if( like == 1 ){
        			$like.text( parseInt( $like.text().trim() ) - 1 );
        			$likeIcon.removeClass().addClass( 'fa fa-thumbs-o-up' );
        		}else{
        			$dislike.text( parseInt( $dislike.text().trim() ) - 1 );
        			$dislikeIcon.removeClass().addClass( 'fa fa-thumbs-o-down' );
        		}
        	}
        },
        error   : function( response ) {
                    alert('Error');     
        }
    } );
    return false;
}


